package com.volkanic.controller;

import com.badlogic.gdx.Gdx;
import com.volkanic.game.BlueMoonGame;
import com.volkanic.thread.ActingThread;
import com.volkanic.views.menu.MenuView;
import com.volkanic.views.worldselection.WorldSelectionView;

/**
 * Created by Lucas on 24/07/2014.
 */
public class GameController {

    /**
     * Tag for debugging
     */
    private final String TAG = "GameController";
    /**
     * Enumeration of the state of the game
     */
    public static enum StageState {
        MENU,
        WORLD_SELECTION,
        HIGHSCORE,
        LEVEL,
        MULTIPALYER,
        EXIT
    }

    /**
     * The thread handling the acting process
     */
    private ActingThread _actingThread;
    /**
     * The libGdx Game
     */
    private BlueMoonGame _game;
    /**
     * The current state of the game
     */
    private StageState _currentState;
    /**
     * Determine if the screen have been set
     */
    private boolean screenIsSet;

    /**
     * Create a new instance of the class SharedController
     * @param state The state of the controller
     */
    public GameController(StageState state){
        // Setup the game
        _game = new BlueMoonGame(this);

        // Setup the thread for motion
        _actingThread = new ActingThread(_game);
        _actingThread.start();

        screenIsSet = false;
        changeStateTo(state);
    }

    /**
     * Change the current state of the game
     * @param state the new state of the game
     */
    public void changeStateTo(StageState state){
        Gdx.app.debug(TAG, "ChangeStateTo");
        this.disposeScreen();
        _currentState = state;
        this.changeStage();
        screenIsSet = true;
    }

    /**
     * Dispose the element included in the screen
     */
    private void disposeScreen(){
        if (screenIsSet){
            _game.getScreen().dispose();
        }
    }

    /**
     * Change the current stage
     */
    private void changeStage(){
        Gdx.app.debug(TAG, "ChangeState");
        switch(_currentState){
            case MENU:
                _game.setScreen(new MenuView(this));
                break;
            case WORLD_SELECTION:
                _game.setScreen(new WorldSelectionView(this));
                break;
            case HIGHSCORE:
                break;
            case LEVEL:
                break;
            case MULTIPALYER:
                break;
            case EXIT:
                _game.dispose();
                Gdx.app.exit();
                break;
            default:
                break;
        }
    }

    /**
     * Return to the previous screen
     */
    public void previousScreen(){
        Gdx.app.debug(TAG, "Previous Screen");
        switch(_currentState){
            case MENU:
                this.changeStateTo(StageState.EXIT);
                break;
            case WORLD_SELECTION:
            case HIGHSCORE:
            case MULTIPALYER:
                this.changeStateTo(StageState.MENU);
                break;
            case LEVEL:
                break;
            default:
                break;
        }
    }

    /**
     * Call the render loop of the game
     */
    public void render(){
        _game.render();
    }

    /**
     * Pause the game
     */
    public void pause() {
        _actingThread.interrupt();
        _game.pause();
    }

    /**
     * Restart the game
     */
    public void restart() {
        _actingThread.restart();
        _game.resume();
    }

    /**
     * Dispose all element of the game
     */
    public void dispose() {
        _actingThread.dispose();
        _game.dispose();
    }
}